package cn.me.alphamq.nameserver.store;

import cn.me.alphamq.common.enumeration.ServiceInstanceTypeEnum;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务实例，包括 producer、consumer、broker
 *
 * @author f
 */
public class ServiceInstance {

    /**
     * 与服务实例连接的 channel 的上下文
     */
    private ChannelHandlerContext ctx;

    /**
     * 服务实例的类型
     */
    private ServiceInstanceTypeEnum serviceInstanceType;

    /**
     * 服务实例 的 ip 地址
     */
    private String ip;

    /**
     * 服务实例 的端口
     */
    private Integer port;

    /**
     * broker “注册”（broker 第一次发送心跳包）的时间戳
     */
    private Long registryTimestamp;

    /**
     * 上一次 broker 发送心跳包的时间戳
     */
    private Long lastHeartBeatTimestamp;

    /**
     * 存储 broker 的元数据
     */
    private Map<String, Object> attrs;

    public ServiceInstance(ChannelHandlerContext ctx, String ip, Integer port,
                           String serviceInstanceType, Map<String, Object> attrs) {
        long currTimestamp = System.currentTimeMillis();
        this.ctx = ctx;
        this.ip = ip;
        this.port = port;
        this.registryTimestamp = currTimestamp;
        this.lastHeartBeatTimestamp = currTimestamp;
        this.serviceInstanceType = ServiceInstanceTypeEnum.of(serviceInstanceType);
        this.attrs = attrs == null ? new HashMap<>() : attrs;
    }

    public ChannelHandlerContext getCtx() {
        return ctx;
    }

    public void setCtx(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    public ServiceInstanceTypeEnum getServiceInstanceType() {
        return serviceInstanceType;
    }

    public void setServiceInstanceType(ServiceInstanceTypeEnum serviceInstanceType) {
        this.serviceInstanceType = serviceInstanceType;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Long getRegistryTimestamp() {
        return registryTimestamp;
    }

    public void setRegistryTimestamp(Long registryTimestamp) {
        this.registryTimestamp = registryTimestamp;
    }

    public Long getLastHeartBeatTimestamp() {
        return lastHeartBeatTimestamp;
    }

    public void setLastHeartBeatTimestamp(Long lastHeartBeatTimestamp) {
        this.lastHeartBeatTimestamp = lastHeartBeatTimestamp;
    }

    public Map<String, Object> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }
}
